package LMR.rbac.service;

import LMR.rbac.Entity.Dept;
import LMR.rbac.Entity.Role;
import LMR.rbac.Entity.User;
import LMR.rbac.dto.Userdto;
import LMR.rbac.vo.Result;
import LMR.rbac.vo.UserVo;

import java.util.Arrays;

public class Userservice {
    public static int Data_Size=100;
    public static User[] data=new User[Data_Size+1];
    public static int idCounter=1;
    public static UserVo convertToVo(User user){
        String deptName="";
        Dept dept=Deptservice.findById(user.deptID);
        if (dept!=null){
            deptName=dept.name;
        }
        String  roleName="";
        Role role=Roleservice.findById(user.roleID);
        if (role!=null){
            roleName=role.name;
        }
        return new UserVo(user.id,user.username,user.name,user.mobile,user.email,user.deptID,user.roleID,deptName,roleName,user.enabled);
     }
    public static User createEntity(Userdto dto, int id){
        User user=new User(dto.username, dto.name, dto.mobile, dto.email, dto.deptID, dto.roleID);
        user.id=id;
        return user;
    }
    public static void updateEntity(User user,Userdto dto){
        user.username=dto.username;
        user.name=dto.name;
        user.mobile=dto.mobile;
        user.email=dto.email;
        user.deptID=dto.deptID;
        user.roleID=dto.roleID;
    }
    public static boolean deptInUse(int deptid){
        boolean inUse=false;
        for (User user:data){
            if(user!=null&&user.deptID==deptid){
                inUse=true;
                break;
            }
        }
        return inUse;
    }
    public static boolean roleInUse(int roleid){
        boolean inUse=false;
        for (User user:data){
            if(user!=null&&user.deptID==roleid){
                inUse=true;
                break;
            }
        }
        return inUse;
    }
    public static boolean existsById(int id){
        return id >= 1 && id < idCounter &&data[id]!=null;
    }
    public static Result list(){
        UserVo[] temp=new UserVo[Data_Size];
        int length=0;
        for (int i=1;i<idCounter;i++){
            if (data[i]!=null){
                temp[length++]=convertToVo(data[i]);
            }
        }
        return Result.success("查询成功", Arrays.copyOf(temp,length));
    }
    public static Result create(Userdto dto){
        if (idCounter>Data_Size){
            return Result.failure("创建失败，数据已满");
        }
        if (!Deptservice.existsById(dto.deptID)){
            return Result.failure("部门不存在");
        }
        if (!Roleservice.existsById(dto.roleID)){
            return Result.failure("角色不存在");
        }
        User user=createEntity(dto,idCounter++);
        data[user.id]=user;
        return Result.success("创建成功",null);
    }
    public static Result update(Userdto dto,int id){
        if (!existsById(id)){
            return Result.failure("没有该ID对应的数据");
        }
        if (!Deptservice.existsById(dto.deptID)){
            return Result.failure("部门不存在");
        }
        if (!Roleservice.existsById(dto.roleID)){
            return Result.failure("角色不存在");
        }
        User user=data[id];
        updateEntity(user,dto);
        return Result.success("更新成功",null);
    }
    public static Result toggle(int id){
        if (!existsById(id)){
            return Result.failure("没有该ID对应的数据");
        }
        data[id].enabled=!data[id].enabled;
        return Result.success("切换成功",null);
    }
}
